import grpc
import quote_pb2
import quote_pb2_grpc


def run():
    # 连接服务器并发送请求
    with grpc.insecure_channel("localhost:50051") as channal:
        # 获得客户端存根
        stub = quote_pb2_grpc.QuoteServiceStub(channel=channal)

        # 添加 metadata ，格式固定：list[tuple]
        metadata = [("token", "secret_token1")]  # 可以改成错误的 token 试试

        # 发起请求
        try:
            response = stub.GetQuote(quote_pb2.Empty(), metadata=metadata)
            print(response.data)
        except grpc._channel._InactiveRpcError as e:
            print(e.code())
            print(e.details())


if __name__ == "__main__":
    run()
